package sk.stuba.fiit.pki.entity;

import java.io.Serializable;
import java.math.BigInteger;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * Document Entity.
 * 
 * @author Marek Hruska
 */
@Entity(name = "CERTIFICATE")
public class CertificateEntity implements Serializable {

	private static final long serialVersionUID = -8712872385957386182L;

	private Integer id = null;
	private BigInteger serialNumber = null;
	private Boolean revocate = null;
	private Date valid_from = null;
	private Date valid_to = null;
	private byte[] cert = null;
	private String issuer = null;
	private String subject = null;
	private String pk_link = null;
	private String revocateInfo = null;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public Date getValid_from() {
		return valid_from;
	}

	public void setValid_from(Date valid_from) {
		this.valid_from = valid_from;
	}

	public Date getValid_to() {
		return valid_to;
	}

	public void setValid_to(Date valid_to) {
		this.valid_to = valid_to;
	}

	public byte[] getCert() {
		return cert;
	}

	public void setCert(byte[] cert) {
		this.cert = cert;
	}

	public String getIssuer() {
		return issuer;
	}

	public void setIssuer(String issuer) {
		this.issuer = issuer;
	}

	public String getPk_link() {
		return pk_link;
	}

	public void setPk_link(String pk_link) {
		this.pk_link = pk_link;
	}

	public BigInteger getSerialNumber() {
		return serialNumber;
	}

	public void setSerialNumber(BigInteger serialNumber) {
		this.serialNumber = serialNumber;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(cert);
		result = prime * result + ((issuer == null) ? 0 : issuer.hashCode());
		result = prime * result
				+ ((serialNumber == null) ? 0 : serialNumber.hashCode());
		result = prime * result
				+ ((valid_from == null) ? 0 : valid_from.hashCode());
		result = prime * result
				+ ((valid_to == null) ? 0 : valid_to.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CertificateEntity other = (CertificateEntity) obj;
		if (!Arrays.equals(cert, other.cert))
			return false;
		if (issuer == null) {
			if (other.issuer != null)
				return false;
		} else if (!issuer.equals(other.issuer))
			return false;
		if (serialNumber == null) {
			if (other.serialNumber != null)
				return false;
		} else if (!serialNumber.equals(other.serialNumber))
			return false;
		if (valid_from == null) {
			if (other.valid_from != null)
				return false;
		} else if (!valid_from.equals(other.valid_from))
			return false;
		if (valid_to == null) {
			if (other.valid_to != null)
				return false;
		} else if (!valid_to.equals(other.valid_to))
			return false;
		return true;
	}

	public Boolean getRevocate() {
		return revocate;
	}

	public void setRevocate(Boolean revocate) {
		this.revocate = revocate;
	}
	
	public void convertCertificateToEntity(X509Certificate certificate) throws Exception{
		this.cert = certificate.getEncoded();
		this.issuer = certificate.getIssuerDN().toString();
		this.subject = certificate.getSubjectDN().toString();
		this.revocate = false;
		this.serialNumber = certificate.getSerialNumber();
		this.valid_from = certificate.getNotBefore(); 
	    this.valid_to = certificate.getNotAfter();
	}

	public String getRevocateInfo() {
		return revocateInfo;
	}

	public void setRevocateInfo(String revocateInfo) {
		this.revocateInfo = revocateInfo;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

}
